// pages/sailOrderInfo/add/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    options: [],
    fieldNames: {
      text: 'areaName',
      value: 'areaNo',
      children: 'areas',
    },
    list: [{
      fieldValue: '',
      cascaderValue: '',
      place: '',
      weight: '',
      totleSize: ''
    }],
    id: '',
    productId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id,
      productId: options.productId
    })
    this.getlengku()
  },

  onClick(event) {
    this.setData({
      show: true,
      activeIndex: event.currentTarget.dataset.index
    });
  },
  getlengku() {
    wx.$req({
      url: '/wms/warehouse/listByBusiness',
      loadingText: '加载中...',
      method: "get"
    }).then(res => {
      console.log(res, '储存库')
      res.data.forEach(item => {
        item.areaName = item.warehouseName
        item.areaNo = item.warehouseNo
      })
      this.setData({
        options: res.data
      })
    })
  },
  gettotalSize(data) {
    const that = this
    console.log(data)
    wx.$req({
      url: `/wms/transactions/getProjectWeightSum/${data[1]}/${that.data.productId}`,
      loadingText: '加载中...',
      method: "get"
    }).then(res => {
      console.log(res.data)
      const checked = "list[" + that.data.activeIndex + "].totleSize"
      that.setData({
        [checked]: res.data
      })
    }).catch(() => {
      const checked = "list[" + that.data.activeIndex + "].totleSize"
      that.setData({
        [checked]: ''
      })
      wx.showToast({
        title: '该产品不存在',
        icon: 'none'
      })
    })
  },
  onClose() {
    this.setData({
      show: false,
    });
  },
  onChangeField(event) {
    var checked = "list[" + event.currentTarget.dataset.index + "].weight"
    this.setData({
      [checked]: event.detail,
    })
  },
  onFinish(e) {
    console.log(e)
    const {
      selectedOptions
    } = e.detail;
    const fieldValue = selectedOptions.map((option) => option.areaName || option.warehouseName).join('/');
    const fieldValue2 = selectedOptions.map((option) => option.id)
    var setfieldValue = "list[" + this.data.activeIndex + "].fieldValue"
    var cascaderValue = "list[" + this.data.activeIndex + "].place"
    var warehouseId = "list[" + this.data.activeIndex + "].warehouseId"
    var areaId = "list[" + this.data.activeIndex + "].areaId"
    this.gettotalSize(fieldValue2)
    this.setData({
      [setfieldValue]: fieldValue,
      [cascaderValue]: fieldValue2,
      [warehouseId]: fieldValue2[0],
      [areaId]: fieldValue2[1],
      show: false
    })
  },
  add() {
    const list = this.data.list
    list.push({
      fieldValue: '',
      cascaderValue: '',
      place: '',
      weight: '',
      totleSize: ''
    })
    console.log(list)
    this.setData({
      list: list
    })
  },
  submit() {
    let that = this
    for (let key in this.data.list) {
      if (this.data.list[key].place.length == 0 || !this.data.list[key].place) {
        wx.showToast({
          title: '请选择库区',
          icon: 'none'
        })
        return
      }
      if (!this.data.list[key].weight) {
        wx.showToast({
          title: '请输入重量',
          icon: 'none'
        })
        return
      }
    }
    wx.$req({
      url: '/wms/orderSail/out',
      loadingText: '加载中...',
      data: {
        id: this.data.id,
        orderOutInJson: JSON.stringify(this.data.list)
      },
      method: "Put"
    }).then(res => {
      if (res.data.code == 500) {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '出库成功',
          icon: 'success'
        })
        setTimeout(() => {
          wx.navigateBack()
        }, 1000)
      }
    }).catch(() => {
      wx.showToast({
        title: '出库失败',
        icon: 'none'
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})